package com.dongdongshop.controller;

import cn.hutool.json.JSONUtil;
import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnums;
import com.dongdongshop.pojo.PageResult;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.vo.TbSpecificationOptionVO;
import com.dongdongshop.service.TbSpecificationOptionSerive;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("TbSpecificationOption")
public class TbSpecificationOptionController {

    //注入service
    @DubboReference
    private TbSpecificationOptionSerive tbSpecificationOptionSerive;

    //查询规格全部数据(要求分页)
    @ResponseBody
    @RequestMapping("TbSpecificationshowall")
    public Result TbSpecificationshowall(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize, String specName) {
        try {
            PageResult<TbSpecification> result = tbSpecificationOptionSerive.TbSpecificationshowall(pageNum, pageSize, specName);
            return Result.response(ResultEnums.SHOW_SUCCESS).setData(result);
        } catch (Exception e) {
            return Result.response(ResultEnums.SHOW_ERROR);
        }
    }

    //查询规格全部数据
    @ResponseBody
    @RequestMapping("TbSpecificationshow")
    public Result TbSpecificationshow() {
        try {
            List<TbSpecification> result = tbSpecificationOptionSerive.TbSpecificationshow();
            return Result.response(ResultEnums.SHOW_SUCCESS).setData(result);
        } catch (Exception e) {
            return Result.response(ResultEnums.SHOW_ERROR);
        }
    }

    //添加规格数据
    @ResponseBody
    @RequestMapping("addTbSpecification")
    public Result addTbSpecification(TbSpecification specName, String s) {
        List<TbSpecificationOption> tbSpecificationOptions = JSONUtil.toList(s, TbSpecificationOption.class);
        try {
            tbSpecificationOptionSerive.addTbSpecification(specName, tbSpecificationOptions);
            return Result.response(ResultEnums.ADD_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.ADD_ERROR);
        }
    }

    //规格数据回显
    @ResponseBody
    @RequestMapping("showecho")
    public Result showecho(Long id) {
        try {
            TbSpecificationOptionVO vo = tbSpecificationOptionSerive.showecho(id);
            return Result.response(ResultEnums.ECHO_SUCCESS).setData(vo);
        } catch (Exception e) {
            System.out.println(e);
            return Result.response(ResultEnums.ECHO_ERROR);
        }
    }

    //修改规格数据
    @ResponseBody
    @RequestMapping("updateTbSpecification")
    public Result updateTbSpecification(TbSpecification specName, String s) {
        List<TbSpecificationOption> tbSpecificationOptions = JSONUtil.toList(s, TbSpecificationOption.class);
        try {
            tbSpecificationOptionSerive.updateTbSpecification(specName, tbSpecificationOptions);
            return Result.response(ResultEnums.UPDATE_SUCCESS);
        } catch (Exception e) {
            System.out.println(e);
            return Result.response(ResultEnums.UPDATE_ERROR);
        }
    }

    //删除单个数据
    @ResponseBody
    @RequestMapping("deleteTbSpecification")
    public Result deleteTbSpecification(Long id) {
        try {
            tbSpecificationOptionSerive.deleteTbSpecification(id);
            return Result.response(ResultEnums.DELETE_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.DELETE_ERROR);
        }
    }

    //删除多个数据
    @ResponseBody
    @RequestMapping("deleteallTbSpecification")
    public Result deleteallTbSpecification(@RequestParam(value = "ids[]") List<Long> ids) {
        try {
            tbSpecificationOptionSerive.deleteallTbSpecification(ids);
            return Result.response(ResultEnums.DELETE_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.DELETE_ERROR);
        }
    }
}
